<?php   

/** 
* Newsroom (Controller) 
* Блок новостей (контроллер) 
* @author IT studio IRBIS-team 
* @copyright © 2009 IRBIS-team 
*/
/////////////////////////////////////////////////////////

/**
* Generation of page of an error at access out of system
* Генерация страницы ошибки при доступе вне системы
*/
    if(!defined('IRB_KEY'))
    {
       header("HTTP/1.1 404 Not Found");      
       exit(file_get_contents('../../404.html'));
    } 
//////////////////////////////////////////////////////////

/**
* We connect a file of the MySQL functions
* Подключаем файл функций MySQL
*/  
   include './libs/mysql.php'; 
   
/**
* We connect a BB-decoder
* Подключаем файл BB-декодер
*/  
   include './bbcode/irb_bbdecoder.php';
      
/**
* News line generation 
* Генерация ленты новостей
*/    

   
    if(!empty($_SERVER['HTTP_REFERER']) 
	    && trim($_SERVER['HTTP_REFERER'], '/') !== trim(IRB_HOST, '/') 
	    && strpos($_SERVER['HTTP_REFERER'], 'main') === false)
	{
        $GET['news'] = 'all';
    }	

    $tpl = getTpl('news/rows');
    $news = '';

    if(is_numeric($GET['news']))
	{
/**
* News generation by id
* Генерация новости по id
*/

        $res = mysqlQuery("SELECT `id`, `subtitle`, `text`,
                           DATE_FORMAT(`date`,'%d') AS `day`, 
                           DATE_FORMAT(`date`,'%m') AS `month`,
                           DATE_FORMAT(`date`,'%Y') AS `year` 
                             FROM `". IRB_DBPREFIX ."news`  
                               WHERE `public` = 1 
                                 AND `id` = " . (int)$GET['news']);



        if (mysql_num_rows($res) > 0)
        {
            $row = mysql_fetch_assoc($res);

            $row['date'] = $row['day'] . ' ' . $lang_month_string[$row['month']] . ' ' . $row['year'];
            $row['subtitle'] = htmlspecialchars($row['subtitle']);
            $row['text'] = createBBtags($row['text']);
            $row['url'] = href('news=all');			
            $row['link'] = IRB_LANG_ALL_NEWS;

            $news = parseTpl($tpl, $row);
        }
        else
        {
            header("HTTP/1.1 404 Not Found");
            exit(file_get_contents(IRB_ROOT . '/404.html'));
        } 

    }
	else
	{	 
             $res = mysqlQuery("SELECT `id`, `date`,`subtitle`,`public`,
                                DATE_FORMAT(`date`,'%d') AS `day`, 
                                DATE_FORMAT(`date`,'%m') AS `month`,
                                DATE_FORMAT(`date`,'%Y') AS `year`, 
                                SUBSTRING_INDEX(`text`,' '," . IRB_NUM_WORD_NEWS_MAIN . ") AS `text`
                                  FROM `". IRB_DBPREFIX ."news`
                                    WHERE `public` = 1  
                                      ORDER BY `id` DESC 
                                         LIMIT ". IRB_NUM_NEWS_MAIN);                       
              
         if(mysql_num_rows($res) > 0)
         {
              while ($row = mysql_fetch_assoc($res))
              {
                  $row['date'] = $row['day'] . ' ' . $lang_month_string[$row['month']] . ' ' . $row['year'];
                  $row['subtitle'] = htmlspecialchars($row['subtitle']);
                  $row['text'] = createBBtags($row['text'], false) . "...";
                  $row['url'] = href('news=' . $row['id']);
                  $row['link'] = IRB_LANG_FULL_NEWS;
                  $news .= parseTpl($tpl, $row);
              }
         }
         else
         {
              $news = IRB_LANG_NO_NEWS;
         }

    }

